package com.mask.im.business.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.Message;
import com.mask.im.business.mapper.MessageMapper;
import com.mask.im.business.model.dto.MessageDTO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.MessageService;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

	@Override
	public RestResponse<List<MessageDTO>> getUserMessage() {
		Integer userInfoId = MaskIMSecurityUtils.getUserId();
		List<MessageDTO> messageVOList = this.baseMapper.getUserMessage(userInfoId);
		List<MessageDTO> messageVOS = messageVOList.stream()
				.sorted(Comparator.comparing(MessageDTO::getCreateTime))
				.filter(item -> {
					if (item.getUserIsDelete() != null) {
						return !Arrays.asList(item.getUserIsDelete().split(",")).contains(userInfoId.toString());
					} else {
						return true;
					}
				}).collect(Collectors.toList());
		return RestResponse.ok(messageVOS);
	}
}

